import Vue from 'vue'
import Vuex from 'vuex'
import {request} from '../services/request.js';

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
		loading: false,
		echarts: [],
		// 所有集团
		groups: [],
		// 工厂，车间，产线，机组
		factorys: [],
		workshops: [],
		products: [],
		units: []
  },
	getters: {
		weekDays(){
			let str = '日一二三四五六',
					today = new Date().getDay(),
					weekDays = [];
			
			for(let i in str){
				weekDays.push('周' + str[(i-0+today)%7]);
			}
			return weekDays;
		}
	},
  mutations: {
		setData(state, {name, data, key}){
			if(key == undefined){
				state[name] = data;
			}else{
				state[name][key] = data;
			}
			// sessionStorage.setItem(name, state[name]);
		},
		pushData(state, {name, data}){
			state[name].push(data);
		},
  },
  actions: {
		async request(context, {url, query, method='get', name, key}){
			context.commit('setData', {name: 'loading', data: true});
			const data = await request(url, query, method);
			if(data) context.commit('setData', {name, data, key});
			context.commit('setData', {name: 'loading', data: false});
		}
  },
  modules: {
  }
})
